<?php
define(EXCLUDE_QUERY, " AND driver_id != '16' AND driver_id != '100' AND driver_id != '101'");

class Points {
	var $driverid;
	var $adjustmentReason;
	var $adjustmentOther;
	var $points;
	var $sendEmail;
	var $emailSubject;
	var $emailBody;
	var $singlePoints;
	var $doublePoints;
	var $triplePoints;
	var $quadruplePoints;
	var $payPeriod;
	var $errors; 
	var $entryBy;
	
	function displayActiveDriverForm(){
		$query = "SELECT * FROM t_drivers WHERE (active = '1' AND durational = '0'" . EXCLUDE_QUERY . ") ORDER BY lastname ASC";
		$result = mysql_query($query);
		$totalDrivers = mysql_num_rows($result);
		$i = 0;
		while ($data = mysql_fetch_array($result)){
			$i++;
			echo "<tr id=\"row" . $data['driver_id'] . "\"";
			if ($this->errors[$data['driver_id']]) echo " style=\"background-color: #FFCCCC\"";
			echo ">\n";
			echo "<td>" . $data['lastname'] . ", " . $data['firstname'] . "</td>\n";
			echo "<td align=\"center\"><input type=\"text\" value=\"" . $this->singlePoints[$data['driver_id']] . "\" onchange=\"totalDriverPoints(" . $data['driver_id'] . ")\" tabindex=\"" . $i . "\" name=\"points[" . $data['driver_id'] . "]\" size=\"6\"></td>\n";
			echo "<td align=\"center\"><input type=\"text\" value=\"" . $this->doublePoints[$data['driver_id']] . "\" onchange=\"totalDriverPoints(" . $data['driver_id'] . ")\" tabindex=\"" . ($i + $totalDrivers) . "\"  name=\"doublePoints[" . $data['driver_id'] . "]\" size=\"6\"></td>\n";
			echo "<td align=\"center\"><input type=\"text\" value=\"" . $this->triplePoints[$data['driver_id']] . "\" onchange=\"totalDriverPoints(" . $data['driver_id'] . ")\" tabindex=\"" . ($i + ($totalDrivers * 2)) . "\"  name=\"triplePoints[" . $data['driver_id'] . "]\" size=\"6\"></td>\n";
			echo "<td align=\"center\"><input type=\"text\" value=\"" . $this->quadruplePoints[$data['driver_id']] . "\" onchange=\"totalDriverPoints(" . $data['driver_id'] . ")\" tabindex=\"" . ($i + ($totalDrivers * 3)) . "\"  name=\"quadruplePoints[" . $data['driver_id'] . "]\" size=\"6\"></td>\n";
			echo "<td align=\"center\"><span id=\"totalPoints" . $data['driver_id'] . "\">" . ($this->singlePoints[$data['driver_id']] + ($this->doublePoints[$data['driver_id']] * 2) + ($this->triplePoints[$data['driver_id']] * 3) + ($this->quadruplePoints[$data['driver_id']] * 4)) . "</td>";
			echo "</tr>\n";
		}
	}
	
	function displayActiveDriverList($selected = ""){
		$query = "SELECT * FROM t_drivers WHERE (active = '1' AND durational = '0'" . EXCLUDE_QUERY . ") ORDER BY lastname ASC";
		$result = mysql_query($query);
		echo "<select name=\"driver\" onchange=\"updateExtraFields()\">\n";
		while ($data = mysql_fetch_array($result)){
			echo "<option value=\"" . $data['driver_id'] . "\"";
			if ($selected == $data['driver_id']) echo " selected=\"selected\"";
			echo ">" . $data['lastname'] . ", " . $data['firstname'] . "</option>\n";
		}
		echo "</select>\n";
	}
	
	function displayActiveDriverJavascript(){
		echo "<script type=\"text/javascript\">\n";
		echo "var drivers = new Array();\n";
		$query = "SELECT * FROM t_drivers WHERE (active = '1' AND durational = '0'" . EXCLUDE_QUERY . ") ORDER BY lastname ASC";
		$result = mysql_query($query);
		while ($data = mysql_fetch_array($result)){
			echo "drivers[" . $data['driver_id'] . "] = \"" . $data['firstname'] . "\";\n";
		}
		echo "</script>\n";

	}
	function getPayPeriodData(){
		$this->singlePoints = $_POST['points'];
		$this->doublePoints = $_POST['doublePoints'];
		$this->triplePoints = $_POST['triplePoints'];
		$this->quadruplePoints = $_POST['quadruplePoints'];
		$this->payPeriod = $_POST['payperiod'];
	}
	
	function insertPayPeriodData(){
		$desc = "Pay Period Ending " . date("F j, Y", $this->payPeriod);
		foreach($this->singlePoints AS $id => $value)
			if (!empty($value)) mysql_query("INSERT INTO t_seniority SET driver_id = '$id', points = '$value', entry_by = '" . $this->entryBy . "', description='" . $desc . "'");
		foreach($this->doublePoints AS $id => $value)
			if (!empty($value)) mysql_query("INSERT INTO t_seniority SET driver_id = '$id', points = '" . $value * 2 . "', entry_by = '" . $this->entryBy . "', description='" . $desc . " - Double Seniority Points'");
		foreach($this->triplePoints AS $id => $value)
			if (!empty($value)) mysql_query("INSERT INTO t_seniority SET driver_id = '$id', points = '" . $value * 3 . "', entry_by = '" . $this->entryBy . "', description='" . $desc . " - Triple Seniority Points'");
		foreach($this->quadruplePoints AS $id => $value)
			if (!empty($value))	mysql_query("INSERT INTO t_seniority SET driver_id = '$id', points = '" . $value * 4 . "', entry_by = '" . $this->entryBy . "', description='" . $desc . " - Quadruple Seniority Points'");
	}
	
	function validatePayPeriodData(){
		if($this->entryBy <= 0) $this->errors["noEntryBy"] = true;
		$noData = true;
		foreach($this->singlePoints AS $id => $value) {
			if (!empty($value) AND !is_numeric($value)) $this->errors[$id] = true;
			if (!empty($value) AND is_numeric($value) AND $value != "0") $noData = false;
		}
		foreach($this->doublePoints AS $id => $value) {
			if (!empty($value) AND !is_numeric($value)) $this->errors[$id] = true;
			if (!empty($value) AND is_numeric($value) AND $value != "0") $noData = false;
		}
		foreach($this->triplePoints AS $id => $value) {
			if (!empty($value) AND !is_numeric($value)) $this->errors[$id] = true;
			if (!empty($value) AND is_numeric($value) AND $value != "0") $noData = false;
		}
		foreach($this->quadruplePoints AS $id => $value) {
			if (!empty($value) AND !is_numeric($value)) $this->errors[$id] = true;
			if (!empty($value) AND is_numeric($value) AND $value != "0") $noData = false;
		}
		if (empty($this->errors) AND !$noData) return true;
		else return false;
	}
	
	function getPointsData(){
		$this->driverid = $_POST['driver'];
		$this->adjustmentReason = $_POST['reason'];
		$this->points = $_POST['points'];
		$this->sendEmail = $_POST['sendEmail'];
		$this->sendMessage = $_POST['sendMessage'];
		$this->emailSubject = $_POST['emailSubject'];
		$this->emailBody = $_POST['emailBody'];
	}
	
	function validatePointsData(){
		if (empty($this->adjustmentReason)) $this->errors['noReason'] = "Please enter the reason for the adjustment.";
		if ($this->points == "0") $this->errors['noPoints'] = "The points value cannot equal 0.";
		if ($this->sendEmail || $this->sendMessage){
			if (empty($this->emailSubject)) $this->errors['noSubject'] = "You must enter a subject for the email/message.";
			if (empty($this->emailBody)) $this->errors['noBody'] = "You must enter a body for the email/message.";
		}
	}
	
	function displayPayPeriod(){
		echo "<select name=\"payperiod\">\n";
		$sevenDays = 60 * 60 * 24 * 7;
		$thisThursday = strtotime("last Thursday");
		if (empty($this->payPeriod) || $this->payPeriod == 0) $this->payPeriod = $thisThursday;
		$it = 0;
		for ($i = 5; $i >= 1; $i--){
			$thurs = $thisThursday - ($i * $sevenDays);
			echo "<option value=\"" . $thurs . "\"";
			if ($this->payPeriod == $thurs) echo " selected";
			echo ">" . date("l F jS, Y", $thurs) . "</option>\n";
			$it++;
			if ($it > 10) break;
		}
		echo "<option value=\"" . $thisThursday . "\"";
		if ($this->payPeriod == $thisThursday) echo " selected";
		echo ">" . date("l F jS, Y", $thisThursday) . "</option>\n";
		echo "</select>";
	}
	
	function getErrors(){
		if ($this->errors) return $this->errors;
		else return false;
	}
	
	function showErrors(){
	echo "<ul>\n";
		foreach($this->errors AS $error)
			echo "<li>" . $error . "</li>\n";
	echo "</ul>";
	}
	
	function setEnteredBy($id) { $this->entryBy = $id; }
}
?>